package com.sxyh;

import java.io.*;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;


/**
 * gradle cache 文件转换为 maven gav坐标
 * C:\Users\Administrator\.gradle\caches\caches\modules-2\files-2.1
 * 现将 %user_home%\。gradle\caches\caches\modules-2\files-2.1 文件拷贝至
 * E:\android\AndroidSDK\Sdk\extras\m2repository\files-2.1\
 */
public class CopyFile {

    static String path = "E:\\android\\AndroidSDK\\Sdk\\extras\\m2repository\\files-2.1\\";//gradle cache 路徑
    static String destPath = "E:\\android\\AndroidSDK\\Sdk\\extras\\m2repository\\files-2\\";//转换成maven gav坐标路径
    static String stopName = "m2repository";

    public static void main(String[] args) {
        ArrayList<HashMap<String, File>> files = getGroupFile(new File(path));
        for (int i = 0; i < files.size(); i++) {
            HashMap<String, File> map = files.get(i);
            for (Map.Entry<String, File> entry : map.entrySet()) {
//                System.out.print("key = " + entry.getKey());
//                System.out.println(", value = " + entry.getValue().getAbsolutePath());
                copyToLastForld(entry.getKey(), entry.getValue().getAbsolutePath());

            }
        }

    }

    public static void copyToLastForld(String sourcePath, String destPath) {
        File file = new File(sourcePath);//com.android.core
        File file2 = new File(destPath);//com/android/core
        System.out.println(file.getAbsolutePath());
        System.out.println(file2.getAbsolutePath() + "\\" + file.getPath());
        if (file.exists()) {
            LinkedList<File> list = new LinkedList<>();
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                File file3 = files[i];
                String file3Path = file3.getName();
                String file3PathNew = file2 + "\\" + file3Path;
                File file4 = new File(file3PathNew);
                if (file2.isDirectory() && !file4.exists()) {
                    System.out.println(file4.getName() + "13213132");
                    //文件夹
                    file4.mkdirs();
                    proceessForld(file3, file4);
                } else {
                    //文件//目前不存在
                }
            }
        }
    }

    public static ArrayList<HashMap<String, File>> getGroupFile(File file) {
        ArrayList<HashMap<String, File>> list = new ArrayList<>();
        if (file.isDirectory()) {
            File[] filesTemp = file.listFiles();
            HashMap<String, File> map = new HashMap<>();
            for (int i = 0; i < filesTemp.length; i++) {
                File file1 = filesTemp[i];
                File dir = null;
                //System.out.println(file1.getName());
                if (file1.getName().contains(".")) {
                    String pathNew = destPath;
                    String fileTmeps[] = file1.getName().split("\\.");
                    for (int j = 0; j < fileTmeps.length; j++) {
                        String fileName = fileTmeps[j];
                        // System.out.println(fileName);
                        pathNew = pathNew + fileName + "\\";
                        dir = new File(pathNew);
                        if (!dir.exists()) {
                            dir.mkdirs();
                            System.out.println(pathNew + "创建成功");
                        }
                    }


                }

                if (dir != null) {
                    map.put(file1.getAbsolutePath(), dir);
                    list.add(map);
                }
            }

        }

        return list;
    }


    private static void proceessForld(File sourceFile, File destFile) {
        File[] files = sourceFile.listFiles();

        for (int i = 0; i < files.length; i++) {
            File file2 = files[i];
            String path = file2.getAbsolutePath();
            String file3Path = file2.getName();
            String destFilePath = destFile.getAbsolutePath() + "\\" + file3Path;
            System.out.println("destFilePath:" + destFilePath);
            File destFile1 = new File(destFilePath);
            System.out.println(destFile1.getName() + "2222");
            System.out.println(destFile1.isDirectory());
            System.out.println(!destFile1.exists());
            if (file2.isDirectory() && !destFile1.exists()) {
                //文件夹
                System.out.println(path + "2333333");
                destFile1.mkdirs();
                // file.mkdirs();
                proceessForld(file2, destFile1);
            } else {
                //文件//目前不存在//判断是否进行拷贝
                try {
                    proceessFile(file2, destFile1);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    private static String[] splitString(String path) {
        String[] lasts = null;
        lasts = path.split("\\\\");
        int count = lasts.length;
        boolean isFirst = true;
        for (int i = 0; i < count; i++) {
            String str = lasts[i];
            if (str != null && str.contains(".")) {
                if (isFirst) {
                    isFirst = false;
//                    System.out.println("\n\n\n\n");
//                    System.out.println("path=" + path + "");
                }
                //              System.out.println("str=" + str + "");
            }
        }
        return lasts;
    }

    private static void proceessFile(File file, File destFile) throws FileNotFoundException {
        if (file != null) {
            String path = file.getAbsolutePath();
            System.out.println(path + "sfsfsfsfsfs");

            if (path != null) {
                String[] lasts = splitString(path);
                if (lasts != null && lasts.length > 0) {
                    int count = lasts.length;
                    String last = lasts[count - 1];
                    String last2 = lasts[count - 2];
                    if (last2 != null && last2.length() > 20) {
                        //拷贝到上一级目录
                        String des = null;
                        if (count < 2) {
                            return;
                        }
                        for (int i = 0; i < count - 2; i++) {
                            if (i == 0) {
                                des = lasts[i];
                            } else {
                                des = des + "\\\\" + lasts[i];
                            }
                        }
                        des = des + "\\\\" + last;
                        String strParentDirectory = destFile.getParent();
                        File parentFile = new File(strParentDirectory);
                        strParentDirectory = parentFile.getParent() + "\\" + last;
                        copy(file, path, strParentDirectory);
                    } else {
                        // System.out.println("source = "+path);
                    }
                    // System.out.println("source = "+path);
                    // System.out.println("des = "+des);
                }
            }
        }
    }

    /**
     * copy动作
     *
     * @param file
     * @param source
     * @param des
     * @throws FileNotFoundException
     */
    private static void copy(File file, String source, String des) throws FileNotFoundException {
        if (file != null) {
            FileInputStream fis = null;
            FileOutputStream fot = null;
            byte[] bytes = new byte[1024];
            int temp = 0;
            File desFile = new File(des);
            if (desFile.exists()) {
                return;
            }
            try {
                fis = new FileInputStream(file);
                fot = new FileOutputStream(desFile);
                while ((temp = fis.read(bytes)) != -1) {
                    fot.write(bytes, 0, temp);
                    fot.flush();


                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fot != null) {
                    try {
                        fot.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }


        }
    }


    public static ArrayList<File> getFile(File file) {
        ArrayList<File> list = new ArrayList<>();
        if (file.isDirectory()) {
            File[] filesTemp = file.listFiles();
            for (int i = 0; i < filesTemp.length; i++) {
                ArrayList<File> result = getFile(filesTemp[i]);
                list.addAll(result);
            }

        } else {
            list.add(file);
        }

        return list;
    }

    public static File creatforld(File file) {
        String path = file.getAbsolutePath();
        if (path != null) {
            String temp = "files-2.1";
            temp = stopName;
            String temS[] = path.split(temp);

            if (temS.length > 0) {
                String pathNew = "";
                for (int i = 0; i < temS.length; i++) {
                    if (i == 0) {
                        pathNew = pathNew + temS[i];
                    } else {
                        pathNew = pathNew + "\\" + temS[i];
                    }
                    if (pathNew != null && !pathNew.equals("")) {
                        File fileForld = new File(pathNew);
                        if (!fileForld.exists()) {
                            fileForld.mkdirs();
                        }
                        return fileForld;
                    }
                }
            }
        }
        return null;
    }
}
